package com.intellagric.service.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.dao.product.ProductCatetoryDao;
import com.intellagric.interfaces.product.ProductCategoryService;
import com.intellagric.mapper.ProductCategoryMapper;
import com.intellagric.pojo.ProductCategory;
import com.intellagric.pojo.ProductCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper mapper;

    @Autowired
    private ProductCatetoryDao dao;

    public ResponseMessage selectProductCategoryById(String id) {
        ResponseMessage msg=new ResponseMessage();
        msg.add("categoryBean",mapper.selectByPrimaryKey(id));
        return msg;
    }

    public ResponseMessage insertProductCategory(ProductCategory ProductCategory) {
        int i=mapper.insertSelective(ProductCategory);
        if (i!=0)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    public ResponseMessage deleteProductCategory(String id) {
        int i=mapper.deleteByPrimaryKey(id);
        if (i!=0)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    public ResponseMessage editProductCategory(ProductCategory ProductCategory) {
        ProductCategoryExample example=new ProductCategoryExample();
        example.createCriteria().andIdEqualTo(ProductCategory.getId());
        int i =mapper.updateByExampleSelective(ProductCategory,example);
        if (i!=0)
            return ResponseMessage.success();
        return ResponseMessage.fail();
    }

    public LayuiDataGridResult getList(int page, int limit) {
        PageHelper.startPage(page,limit);
        ProductCategoryExample example=new ProductCategoryExample();
        example.setOrderByClause("sort ASC");
        example.createCriteria().andIdIsNotNull();
        List<ProductCategory> list=mapper.selectByExample(example);
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(list,info.getTotal());
    }

    private LayuiDataGridResult getCategoryByParentId(String parentId){
        ProductCategoryExample example=new ProductCategoryExample();
        example.setOrderByClause("sort ASC");
        example.createCriteria().andParentIdEqualTo(parentId);
        List<ProductCategory> list=mapper.selectByExample(example);
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(list,info.getTotal());
    }

    private List<ProductCategory> getChild(String parentId){
        List<ProductCategory> children=getCategoryByParentId(parentId).getData();
        for (ProductCategory category:children){
            category.setChildren(getChild(category.getId()));
        }
        //排序
        Collections.sort(children, new Comparator<ProductCategory>() {
            @Override
            public int compare(ProductCategory o1, ProductCategory o2) {
                return o1.getSort() < o2.getSort() ? -1 : 1;//升序排列
            }
        });
        return children;
    }

    public LayuiDataGridResult getMenu() {
        LayuiDataGridResult result=new LayuiDataGridResult();
        //查询顶级菜单
        List<ProductCategory> rootCategoryList = this.getCategoryByParentId("0").getData();

        //生成树形菜单数据，为一级菜单设置子菜单，getChild是递归调用的
        for (ProductCategory agriecomProduceCategory : rootCategoryList) {
            agriecomProduceCategory.setChildren(getChild(agriecomProduceCategory.getId()));
        }
        result.setData(rootCategoryList);
        return result;
    }

    public LayuiDataGridResult batchDeleteProductCategory(String[] ids) {
        int i=dao.batchDeleteByPrimaryKey(ids);
        if (i!=0)
            return LayuiDataGridResult.success();
        return LayuiDataGridResult.fail();
    }

    public LayuiDataGridResult getProductCategoryByKeyword(int page, int limit, String keyword) {
        PageHelper.startPage(page,limit);
        ProductCategoryExample example=new ProductCategoryExample();
        example.createCriteria().andNameLike("%"+keyword+"%");
        List<ProductCategory> list=mapper.selectByExample(example);
        PageInfo info=new PageInfo(list);
        return LayuiDataGridResult.success().add(list,info.getTotal());
    }
}
